package com.zhj.structure.tree.utils;

/**
 * @PackageName:com.zhj.structure.tree.utils
 * @ClassName:TreeNode
 * @auter: 朱慧杰
 * @date:2020/8/10 0010 10:06
 **/
public class TreeNode implements Comparable<TreeNode> {

    // 节点的权
    private int value;
    private Byte data;

    // 左子节点
    TreeNode leftNode;
    // 右子节点
    TreeNode rightNode;

    public TreeNode() {

    }

    public TreeNode(Byte data,int weight) {
        this.data = data;
        this.value = weight;
    }

    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }

    public Byte getData() {
        return data;
    }

    public void setData(Byte data) {
        this.data = data;
    }

    public TreeNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(TreeNode leftNode) {
        this.leftNode = leftNode;
    }

    public TreeNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(TreeNode rightNode) {
        this.rightNode = rightNode;
    }

    public TreeNode(int value) {
        this.value = value;
    }

    /**
     * 向子树添加节点
     * @param node
     */
    public void add(TreeNode node) {
        if (node == null) {
            return;
        }
        if (node.getValue() < this.value) {
            if (this.leftNode == null) {
                this.leftNode = node;
            } else {
                this.leftNode.add(node);
            }
        } else {
            if (this.rightNode == null) {
                this.rightNode = node;
            } else {
                this.rightNode.add(node);
            }
        }
    }

     /*
        -------------------遍历二叉树
        递归的思想：
        前序遍历 ： 1, 2,4,5, 3,6,7
        中序遍历 ： 4,2,5, 1, 6,3,7
        后序遍历 ： 4,5,2, 6,7,3, 1
     */

    public void frontShow() {
        // 遍历当前节点
        System.out.print(value + "\t");
        // 左节点
        if(leftNode!=null) {
            leftNode.frontShow();
        }
        // 右节点
        if(rightNode!=null) {
            rightNode.frontShow();
        }
    }

    public void midShow() {
        // 左节点
        if(leftNode!=null) {
            leftNode.midShow();
        }
        // 遍历当前节点
        System.out.print(value + "\t");
        // 右节点
        if(rightNode!=null) {
            rightNode.midShow();
        }
    }

    public void rearShow() {
        // 左节点
        if(leftNode!=null) {
            leftNode.rearShow();
        }
        // 右节点
        if(rightNode!=null) {
            rightNode.rearShow();
        }
        // 遍历当前节点
        System.out.print(value + "\t");
    }

    public TreeNode frontSearch(int i) {
        TreeNode target = null;
        if (this.value == i) {
            return this;
        } else {
            if (leftNode != null) {
                target = leftNode.frontSearch(i);
            }
            if (target != null) {
                return target;
            }
            if (rightNode != null) {
                target = rightNode.frontSearch(i);
            }
        }
        return target;
    }

    /**
     * 查找节点
     * @param value
     * @return
     */
    public TreeNode search(int value) {
        if (this.value == value) {
            return this;
        } else if(value < this.value) {
            if (leftNode == null) {
                return null;
            }
            return leftNode.search(value);
        } else {
            if (rightNode == null) {
                return null;
            }
            return rightNode.search(value);
        }
    }

    // 删除结点
    public void deleteNode(int value) {

    }

    // 删除子树
    public void delete(int i) {
        TreeNode parent = this;
        // 判断左
        if (parent.leftNode != null && parent.leftNode.value == i) {
            parent.leftNode = null;
            return;
        }
        // 判断右
        if (parent.rightNode != null && parent.rightNode.value == i) {
            parent.rightNode = null;
            return;
        }
        // 递归检查
        parent = leftNode;
        if (parent != null) {
            parent.delete(i);
        }
        parent = rightNode;
        if (parent != null) {
            parent.delete(i);
        }
    }

    @Override
    public int compareTo(TreeNode o) {
        return o.value - this.value;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "value=" + value +
                ", data=" + data +
                '}';
    }

    public TreeNode searchParent(int value) {
        if ((this.leftNode != null && this.leftNode.value == value) || (this.rightNode != null && this.rightNode.value == value)) {
            return this;
        } else {
            if (this.value > value && this.leftNode != null) {
                return this.leftNode.searchParent(value);
            } else if (this.value <= value && this.rightNode != null){
                return this.rightNode.searchParent(value);
            }
            return null;
        }
    }
}
